package com.clkj.tramcarlibrary.aroundtaxi;

import android.support.annotation.NonNull;
import android.util.Log;

import com.clkj.tramcarlibrary.constant.Constants;
import com.clkj.tramcarlibrary.entity.AroundTaxiInfo;
import com.clkj.tramcarlibrary.http.HttpService;
import com.clkj.tramcarlibrary.schedulers.BaseSchedulerProvider;
import com.clkj.tramcarlibrary.util.CommonUtil;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/1/23.
 */

public class AroundTaxiPresenter  implements AroundTaxiContract.Presenter {


    @NonNull
    AroundTaxiContract.View mAroundTaxiView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeSubscription mSubscriptions;


    public AroundTaxiPresenter(@NonNull AroundTaxiContract.View mAroundTaxiView, @NonNull BaseSchedulerProvider mSchedulerProvider, @NonNull HttpService mHttpService) {
        this.mAroundTaxiView = mAroundTaxiView;
        this.mSchedulerProvider = mSchedulerProvider;
        this.mHttpService = mHttpService;
        this.mSubscriptions = new CompositeSubscription();

        this.mAroundTaxiView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
        mSubscriptions = null;
    }

    @Override
    public void getAroundTaxiList(String lon, String lat, String radius) {
        mAroundTaxiView.showLoading();

        Subscription subscription = mHttpService.getAroundTaxiInfo(lon,lat,radius)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        Log.e("aroundTaxiInfoList:", jsonObject.toString());
                        if (!jsonObject.get(Constants.RESPONSE_KEY_STATUS).getAsString().equals(Constants.SUCCESS_VALUE))
                        {
                            mAroundTaxiView.dismissLoading();
                            mAroundTaxiView.onGetAroundTaxiListError(jsonObject.get(Constants.RESPONSE_KEY_MESSAGE).getAsString());
                        }

                        return jsonObject.get(Constants.RESPONSE_KEY_STATUS).getAsString().equals(Constants.SUCCESS_VALUE);
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mAroundTaxiView.dismissLoading();
                        mAroundTaxiView.onGetAroundTaxiListError("服务器获取数据失败");
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {

                        List<AroundTaxiInfo> aroundTaxiList = new ArrayList<AroundTaxiInfo>();
                        int aroundTaxiCount = jsonObject.get("count").getAsInt();

                        if (jsonObject.get(Constants.RESPONSE_KEY_DATA).isJsonNull()==false)
                        {
                            aroundTaxiList = CommonUtil.formatJsonToList(jsonObject,Constants.RESPONSE_KEY_DATA,new TypeToken<List<AroundTaxiInfo>>(){});
                        }

                        mAroundTaxiView.dismissLoading();
                        mAroundTaxiView.onGetAroundTaxiListSuccess(aroundTaxiList,aroundTaxiCount);
                    }
                });
        mSubscriptions.add(subscription);
    }
}